<style scoped lang="scss">
.tiktok-loading {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #000;
  z-index: 9999;

  &.fullscreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }

  .loading-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 30px;
  }

  .tiktok-logo {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
  }

  .music-icon {
    display: flex;
    align-items: flex-end;
    height: 30px;
    gap: 4px;

    .music-bar {
      width: 6px;
      background: linear-gradient(to top, #ff0050, #ff2d70);
      border-radius: 3px;
      animation: musicWave 1.5s ease-in-out infinite;

      &.bar1 {
        height: 10px;
        animation-delay: 0s;
      }

      &.bar2 {
        height: 20px;
        animation-delay: 0.2s;
      }

      &.bar3 {
        height: 15px;
        animation-delay: 0.4s;
      }
    }
  }

  .logo-text {
    color: #fff;
    font-size: 24px;
    font-weight: bold;
    letter-spacing: 2px;
    text-shadow: 0 0 10px rgba(255, 0, 80, 0.5);
  }

  .loading-progress {
    width: 200px;
    height: 4px;
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 2px;
    overflow: hidden;
    position: relative;

    .progress-bg {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: linear-gradient(90deg,
          rgba(255, 255, 255, 0.1) 0%,
          rgba(255, 255, 255, 0.3) 50%,
          rgba(255, 255, 255, 0.1) 100%);
      animation: shimmer 2s infinite linear;
    }

    .progress-fill {
      height: 100%;
      background: linear-gradient(90deg, #ff0050, #ff2d70);
      border-radius: 2px;
      transition: width 0.3s ease;
      position: relative;
      z-index: 1;

      &::after {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        width: 10px;
        height: 100%;
        background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.5), transparent);
        animation: progressShine 1.5s infinite;
      }
    }
  }

  .loading-text {
    color: rgba(255, 255, 255, 0.8);
    font-size: 14px;
    letter-spacing: 1px;
    animation: textPulse 2s infinite;
  }

  @keyframes musicWave {

    0%,
    100% {
      transform: scaleY(1);
    }

    50% {
      transform: scaleY(0.3);
    }
  }

  @keyframes shimmer {
    0% {
      transform: translateX(-100%);
    }

    100% {
      transform: translateX(100%);
    }
  }

  @keyframes progressShine {
    0% {
      transform: translateX(0);
      opacity: 0;
    }

    50% {
      opacity: 1;
    }

    100% {
      transform: translateX(200px);
      opacity: 0;
    }
  }

  @keyframes textPulse {

    0%,
    100% {
      opacity: 0.6;
    }

    50% {
      opacity: 1;
    }
  }
}
</style>
<template>
  <div class="tiktok-loading" :class="{ 'fullscreen': fullscreen }">
    <div class="loading-content">
      <!-- 抖音Logo动画 -->
      <div class="tiktok-logo">
        <div class="music-icon">
          <div class="music-bar bar1"></div>
          <div class="music-bar bar2"></div>
          <div class="music-bar bar3"></div>
        </div>
        <div class="logo-text">抖音</div>
      </div>

      <!-- 加载进度条 -->
      <div class="loading-progress">
        <div class="progress-bg"></div>
        <div class="progress-fill" :style="{ width: progress + '%' }"></div>
      </div>

      <!-- 加载提示文字 -->
      <div class="loading-text">
        {{ loadingText }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'

// 定义组件属性
const props = defineProps({
  // 是否全屏显示
  fullscreen: {
    type: Boolean,
    default: true
  },
  // 加载文字
  text: {
    type: String,
    default: '正在加载...'
  },
  // 自动显示进度
  autoProgress: {
    type: Boolean,
    default: true
  },
  // 进度更新速度（毫秒）
  progressSpeed: {
    type: Number,
    default: 50
  }
})

// 响应式数据
const progress = ref(0)
const loadingText = ref(props.text)
const intervalId = ref(null)

// 开始加载进度
const startProgress = () => {
  if (props.autoProgress) {
    intervalId.value = setInterval(() => {
      if (progress.value < 100) {
        progress.value += Math.random() * 5
        if (progress.value > 100) progress.value = 100

        // 根据进度更新文字
        if (progress.value < 30) {
          loadingText.value = '正在连接...'
        } else if (progress.value < 70) {
          loadingText.value = '加载内容中...'
        } else {
          loadingText.value = '即将完成...'
        }
      } else {
        clearInterval(intervalId.value)
      }
    }, props.progressSpeed)
  }
}

// 设置进度
const setProgress = (value) => {
  progress.value = Math.min(100, Math.max(0, value))
}

// 重置加载
const reset = () => {
  progress.value = 0
  loadingText.value = props.text
  if (intervalId.value) {
    clearInterval(intervalId.value)
    intervalId.value = null
  }
  if (props.autoProgress) {
    startProgress()
  }
}

// 组件挂载时开始进度
onMounted(() => {
  startProgress()
})

// 监听属性变化
watch(() => props.text, (newVal) => {
  loadingText.value = newVal
})

// 暴露方法给父组件
defineExpose({
  setProgress,
  reset
})
</script>
